let Tool = {
    log: function(value){
        switch (typeof value) {
            case "object":
                window.console.log(JSON.stringify(value));
                break;
            default:
                window.console.log(value);
                break;
        }
    },
    getTimestamp: function(isMicro = false) {
        let _timestamp = (new Date()).valueOf();
        if (isMicro)return _timestamp;
        _timestamp = _timestamp/1000;
        _timestamp = Math.floor(_timestamp);
        return _timestamp
    },
    isJsonObj: function(obj) {
        return typeof (obj) === "object" && Object.prototype.toString.call(obj).toLowerCase() === "[object object]" && !obj.length;
    },
    isEmptyObj: function(obj) {
        return JSON.stringify(obj) === '{}'
    },
    objectToQueryString(obj) {
        const parts = [];
        for (const key in obj) {
            if (obj.hasOwnProperty(key)) {
                const value = obj[key];
                if (Array.isArray(value)) {
                    // 如果值是数组，遍历数组并添加每个元素
                    value.forEach((item) => {
                        parts.push(encodeURIComponent(key) + '[]=' + encodeURIComponent(item));
                    });
                } else {
                    // 否则直接添加键值对
                    parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
                }
            }
        }
        return parts.join('&');
    },
    vueArrayClear: function (arr) {
        let length = arr.length;
        arr.splice(0, length);
    },
    vueArrayReset: function (arr, arrVal) {
        this.vueArrayClear(arr);
        arrVal.forEach(function (v) {
            arr.push(v);
        })
    },
    foreach: function(x, fn) {
        for (let k in x) {
            let result = fn.call(x, k, x[k]);
            if (result === false) {
                break;
            }
        }
    },
    async blobToBase64(blob) {
        let resp = await fetch(blob);
        let imgBlob = await resp.blob();
        return new Promise((reslove, reject) => {
            let fileReader = new FileReader();
            fileReader.readAsDataURL(imgBlob);
            fileReader.onload = function (e) {
                reslove(e.target.result);
            }
        });
    },
    base64toBlob: function (base64Data) {
        let byteString;
        if(base64Data.split(',')[0].indexOf('base64') >= 0)
            byteString = atob(base64Data.split(',')[1]);//base64 解码
        else{
            byteString = unescape(base64Data.split(',')[1]);
        }
        let mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];//mime类型 -- image/png
        let ia = new Uint8Array(byteString.length);//创建视图
        for(let i = 0; i < byteString.length; i++) {
            ia[i] = byteString.charCodeAt(i);
        }
        return new Blob([ia], {
            type: mimeString
        });
    },
    moneyFormat: function (num, decimal = 2, split = ',') {
        function thousandFormat (num) {
            const len = num.length
            return len <= 3 ? num : thousandFormat(num.substr(0, len - 3)) + split + num.substr(len - 3, 3)
        }
        if (isFinite(num)) {
            if (num === 0) {
                return num.toFixed(decimal)
            } else {
                var res = ''
                var dotIndex = String(num).indexOf('.')
                if (dotIndex === -1) { // 整数
                    res = thousandFormat(String(num)) + '.' + '0'.repeat(decimal)
                } else {
                    const numStr = String((Math.round(num * Math.pow(10, decimal)) / Math.pow(10, decimal)).toFixed(decimal))
                    const decimals = numStr.slice(dotIndex, dotIndex + decimal + 1) // 截取小数位
                    res = thousandFormat(numStr.slice(0, dotIndex)) + decimals
                }
                return res
            }
        } else {
            return '--'
        }
    },
    // 公共方法-文本复制-h5
    copyText(text, message = '内容已复制'){
        let _this = this;
        if(!text){
            return false;
        }
        text = text.toString();
        let input = document.createElement("input");
        input.value = text;
        document.body.appendChild(input);
        input.select();
        input.setSelectionRange(0, input.value.length);
        document.execCommand('Copy');
        document.body.removeChild(input);
        uni.showToast({
            title:message,
            icon:"none"
        })
    }
}
export default Tool;
